﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PrinterApplication.frmbase;
using FastReport;

namespace PrinterApplication
{
    public partial class MainFrm : BaseFrm
    {
        public MainFrm()
            : base()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 显示预览界面
            CreateReport(true);
        }

        private void CreateReport(bool tfDesigin)
        {
            // 获得当前程序的运行路径
            string path = Application.StartupPath;
            // 定义报表
            Report report = new Report();
            string strDirectory = path + "\\ReportFiles";

            // 判断文件路径是否存在，不存在则创建文件夹
            if (!Directory.Exists(strDirectory))
            {
                // 不存在就创建目录
                Directory.CreateDirectory(strDirectory);
            }

            // 判断文件是否存在
            if (!File.Exists(strDirectory + "\\产品明细.frx"))
            {
                report.FileName = strDirectory + "\\产品明细.frx";
            }
            else
            {
                report.Load(strDirectory + "\\产品明细.frx");
            }

            // 创建报表文件的数据源
            DataSet ds = new DataSet();
            DataTable dt = GetDataSource();
            DataTable dtSource = dt.Copy();
            dtSource.TableName = "ProductDetail";
            ds.Tables.Add(dtSource);
            report.RegisterData(ds);

            if (tfDesigin)
            {
                // 打开设计界面
                report.Design();
            }
            else
            {
                // 打开预览界面
                report.Show();
            }
        }

        private DataTable GetDataSource()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BusinessID", System.Type.GetType("System.Int32"));
            dt.Columns.Add("BusinessName", System.Type.GetType("System.String"));
            dt.Columns.Add("BusinessStatus", System.Type.GetType("System.Int32"));
            dt.Columns.Add("BusinessLogoUrl", System.Type.GetType("System.String"));
            dt.Rows.Add(new object[] { 1, "废话充值", 0, "../Images/Business/1.png" });
            dt.Rows.Add(new object[] { 2, "口水缴费", 0, "../Images/Business/2.png" });
            return dt;
        }

        private void btn_Show_Click(object sender, EventArgs e)
        {
            // 显示预览界面
            CreateReport(false);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateReport(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.groupBox1.ResumeLayout(false);
        }

        private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"确定退出？", @"提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void CenterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var centerFrm = new CenterFrm();
            centerFrm.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            var centerFrm = new CenterFrm();
            this.Hide();
            centerFrm.ShowDialog();
            this.Show();
        }
    }
}
